const app = getApp()

Page({
	data: {
		is_iphonex: app.globalData.is_iphonex,
		navbarData: {
			showCapsule: 1, 
			title: '体检报告'
		},
		navHeight: app.globalData.statusBarHeight * 2 + 86,
		listData: null,
		taskInfo: null,
		hideHistory: false,
		doctorName: '',
		none: true
	},
	onLoad: function (options) {
		this.showLoading()
		let getLastRecord = this.getLastRecord(options.taskId)
    let getUserInfo   = this.getCurrentUserInfo()
    console.log("getLastRecord",getLastRecord);
    console.log("getUserInfo",getUserInfo);
		Promise.all([getLastRecord, getUserInfo])
				.then(res => {
					if (!res[0]) {
						return this.setData({ none: true })
					}
					const baseInfo = {
						'姓名': { OnlyData: res[1].userName },
						'性别': { OnlyData: (res[1].gender === 'F' ? '女' : '男') },
						'年龄': { OnlyData: this.getAge(res[1].birthday, res[0].taskInfo) },
						'科室': { OnlyData: res[1].deptName },
						'医师': { OnlyData:  res[0].taskInfo ? res[0].taskInfo.updateAuname : '医生' }
					}
					const result = {
						'基本信息': baseInfo,
						...res[0].dataContent
					}
					this.setData({ 
						listData: result, taskInfo: res[0].taskInfo, 
						doctorName: res[0].taskInfo ? res[0].taskInfo.updateAuname : '医生',
						none: false
					})
					wx.hideLoading()
				})
				.catch(err => {
					console.log(err)
					wx.hideLoading()
					this.showToast('拉取体检报告失败')
				})
				.finally(() => {
					wx.hideLoading()
				})
	},
	/**
	 * 去历史报告列表界面
	 */
	handleToHistory: function () {
		wx.navigateTo({
		  url: '/pages/report/bodyCheckup/historyBodyCheckup/historyBodyCheckup',
		})
	},
	/**
	 * 获取最后一次记录
	 * @param { string } taskId 任务 Id
	 */
	getLastRecord: function (taskId = '') {
		console.log(app.globalData)
		return new Promise((resolve, reject) => {
			this.setData({ hideHistory: taskId !== '' })
			wx.request({
				url: app.getUrl('/xcx/guidance/queryGuidanceDetail'),
				method: 'GET',
				header: wx.getStorageSync('cookie'),
				data: {
					taskId,
					templType: 0,
					deptId: app.globalData.deptId
				},
				success: function (res) {
					res.data.code === 0 ? resolve(res.data.data) : reject()
				},
				fail: function (err) {
					reject(err)
				}
			})
		})
	},
	/**
	 * 获取当前的用户详细信息
	 */
	getCurrentUserInfo() {
		return new Promise((resolve, reject) => {
      let {openid,accountId} = wx.getStorageSync('logindata')
			wx.request({
        // url: app.getUrl('/xcx/pt/getCurrentUserInfo'),
        url: app.getUrl(`/xcx/pt/getCurrentUserInfo?openId=${openid}&accountId=${accountId}`),
			  method: 'GET',
			  header: wx.getStorageSync('cookie'),
			  success: function (res) {
					res.data.code === 0 ? resolve(res.data.data) : reject()
			  },
			  fail: function (err) {
				reject(err)
			  }
			})
		})
	},
	/**
	 * 根据出生日期获取年纪
	 * @param { string } birthday 出生日期
	 */
	getAge(birthday, taskInfo) {
		birthday = birthday.replace(/-/g, '/')
		let finishTime = taskInfo.finishDate.replace(/-/g, '/')
		let birthDate = new Date(birthday).getTime(),
		    currentDate = new Date(finishTime).getTime(),
				reduce = Math.floor((currentDate - birthDate) / 1000)
		// 如果是小于三个月显示几周几天
		if (reduce < 3 * 30 * 24 * 3600) {
			let week = parseInt(reduce / 3600 / 24 / 7)
			let day = parseInt(reduce % (3600 * 24 * 7) / 3600 / 24)
			return day ? `${ week }周${ day }天` : `${ week }周`
		// 如果是小于 1 年 且大于 3 个月的显示几月几天
		} else if (reduce < 12 * 30 * 24 * 3600){
			let month = parseInt(reduce / 3600 / 24 / 30)
			let day = parseInt(reduce % (3600 / 24 / 30) / 3600 / 24)
			return day ? `${ month }月${ day }天` : `${ month }月`
		// 如果大于 1 岁 小于 6 岁 显示几年零几月
		}	else if (reduce < 6 * 12 * 30 * 24 * 3600) {
			let year =  parseInt(reduce / 3600 / 24 / 30 / 12)
			let month = parseInt((reduce - year * 3600 * 24 * 30 * 12) / 3600 / 24 / 30)
			return month ? `${ year }岁${ month }月` : `${ year }岁`
		// 显示整型岁数	
		}	else {
			let year =  parseInt(reduce / 3600 / 24 / 30 / 12)
			return `${ year }岁`
		}
	},
	/**
	 * 显示加载
	 */
	showLoading: function () {
		wx.showLoading({
		  title: '加载中...',
		  icon: 'none'
		})
	},
	/**
	 * 显示提示文字
	 * @param {*} msg 
	 */
	showToast: function (msg) {
		wx.showToast({
		  title: msg,
		  icon: 'none'
		})
	}
})